package org.jeecg.modules.demo.yxadjust.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.demo.pubuyorder.service.IPuBuyOrderService;
import org.jeecg.modules.demo.utils.generateOddUtil;
import org.jeecg.modules.demo.yxadjust.entity.YxAdjust;
import org.jeecg.modules.demo.yxadjust.service.IYxAdjustService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

 /**
 * @Description: 调整单
 * @Author: jeecg-boot
 * @Date:   2022-06-09
 * @Version: V1.0
 */
@Api(tags="调整单")
@RestController
@RequestMapping("/yxadjust/yxAdjust")
@Slf4j
public class YxAdjustController extends JeecgController<YxAdjust, IYxAdjustService> {
	@Autowired
	private IYxAdjustService yxAdjustService;
	@Autowired
	private IPuBuyOrderService puBuyOrderService;

	/**
	 * 分页列表查询
	 *
	 * @param yxAdjust
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "调整单-分页列表查询")
	@ApiOperation(value="调整单-分页列表查询", notes="调整单-分页列表查询")
	@GetMapping(value = "/list")
	@PermissionData(pageComponent="yunxiao/yxadjust/YxAdjustList")
	public Result<?> queryPageList(YxAdjust yxAdjust,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<YxAdjust> queryWrapper = QueryGenerator.initQueryWrapper(yxAdjust, req.getParameterMap());
		Page<YxAdjust> page = new Page<YxAdjust>(pageNo, pageSize);
		IPage<YxAdjust> pageList = yxAdjustService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param yxAdjust
	 * @return
	 */
	@AutoLog(value = "调整单-添加")
	@ApiOperation(value="调整单-添加", notes="调整单-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody YxAdjust yxAdjust,HttpServletRequest req) {
		try{
			String tenantId = req.getHeader("tenant-id");
			String precode = puBuyOrderService.getTeantCodeById(Integer.parseInt(tenantId)).toUpperCase();
			String str = yxAdjustService.getMaxCode();
			Map<String,String> map = new HashMap<>();
			if(str==null){
				map.put("maxCode",null);
			}else{
				map.put("maxCode",str);
			}
			String name = precode+"TZ_";
			String resultCode = generateOddUtil.createCode(map,name);
			yxAdjust.setAdjustNo(resultCode);
			yxAdjust.setShenhe(0);
			yxAdjust.setShanchu(0);
			yxAdjust.setCreateTime(new Date());
			yxAdjustService.save(yxAdjust);
			return Result.OK("成功！");
		}catch (Exception e){
			e.printStackTrace();
			return Result.error(500,"系统错误！");
		}

	}
	 /**
	  *  审核
	  *
	  * @param yxAdjust
	  * @return
	  */
	 @AutoLog(value = "调整单-审核")
	 @ApiOperation(value="调整单-审核", notes="调整单-审核")
	 @PostMapping(value = "/checkAdjust")
	 public Result<?> checkAdjust(@RequestBody YxAdjust yxAdjust) {
		try{

			// 获取登录用户信息
			LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
			if(!sysUser.getId().equals(yxAdjust.getSaleman())){
				return Result.error(500,"您不是当前单据的指定审核人！");
			}
			if(yxAdjust.getShenhe()==1){
				return Result.error(500,"单据已审核！");
			}
			if(yxAdjustService.checkAdjust(yxAdjust)){
				return Result.OK("审核成功!");
			}
			return Result.error(500,"审核失败!");
		}catch (Exception e){
			e.printStackTrace();
			return Result.error(500,"系统错误！");
		}
	 }


	 /**
	  *  弃审
	  *
	  * @param yxAdjust
	  * @return
	  */
	 @AutoLog(value = "调整单-弃审")
	 @ApiOperation(value="调整单-弃审", notes="调整单-弃审")
	 @PostMapping(value = "/unCheckAdjust")
	 public Result<?> unCheckAdjust(@RequestBody YxAdjust yxAdjust) {
		 try{
			 // 获取登录用户信息
			 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
			 if(!sysUser.getId().equals(yxAdjust.getSaleman())){
				 return Result.error(500,"您不是当前单据的指定审核人！");
			 }
			 if(yxAdjust.getShenhe()==0){
				 return Result.error(500,"请先审核单据！");
			 }
			 if(yxAdjustService.unCheckAdjust(yxAdjust)){
				 return Result.OK("弃审成功!");
			 }
			 return Result.error(500,"弃审失败！");
		 }catch (Exception e){
			 e.printStackTrace();
			 return Result.error(500,"系统错误！");
		 }
	 }

	/**
	 *  编辑
	 *
	 * @param yxAdjust
	 * @return
	 */
	@AutoLog(value = "调整单-编辑")
	@ApiOperation(value="调整单-编辑", notes="调整单-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody YxAdjust yxAdjust) {
		yxAdjustService.updateById(yxAdjust);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "调整单-通过id删除")
	@ApiOperation(value="调整单-通过id删除", notes="调整单-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		try{
			YxAdjust adjust = yxAdjustService.getById(id);
			if(adjust.getShenhe()==1){
				return Result.error(500,"单据已审核，无法删除!");
			}
			yxAdjustService.removeById(id);
			return Result.OK("删除成功!");
		}catch (Exception e){
			e.printStackTrace();
			return Result.error(500,"系统错误!");
		}
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "调整单-批量删除")
	@ApiOperation(value="调整单-批量删除", notes="调整单-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.yxAdjustService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "调整单-通过id查询")
	@ApiOperation(value="调整单-通过id查询", notes="调整单-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		YxAdjust yxAdjust = yxAdjustService.getById(id);
		if(yxAdjust==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(yxAdjust);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param yxAdjust
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, YxAdjust yxAdjust) {
        return super.exportXls(request, yxAdjust, YxAdjust.class, "调整单");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, YxAdjust.class);
    }

}
